<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn1">点击开始</button>
    <button id="btn2">点击停止</button>
    <script>
        var myTimer;
        btn1.onclick = function () {
            myTimer = setInterval(function () {
                document.body.style.background = 'rgb(' + randomColor(0, 255) + ',' + randomColor(0, 255) + ',' + randomColor(0, 255) + ')'
            }, 1000)

            btn2.onclick = function () {
                clearInterval(myTimer)
            }
            function randomColor(m, n) {
                var max, min,
                    max = n > m ? n : m;
                min = m < n ? m : n;
                return parseInt(Math.random() * (max - min + 1) + min)
            }
        }
    </script>
</body>

</html>